7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GeminiCLIを使用してMCPサーバを作って起動して呼び出してみる

7
Last updated at Posted at 2025-12-04

1.はじめに

公開されたMCPサーバを初めて使用して、「MCPサーバって便利!」って思った人も多いと思います。

しかし、いざ作ってみようとしても、「どうしたらいいのかわからない」って人も多いと思います。

今回は、コード生成AIを使用してMCPサーバを作って起動し、ClaudeDesktopを使用して呼び出してみるまでをやってみようと思います。

※今回は無料の範囲でやってみます。

2.環境

以下の環境にて実施します。

・VSCode 1.105.1
・Python 3.11
・GeminiCLI
・ClaudeDesktop
・uv仮想環境

3. 手順

MCPサーバの構築

GeminiCLIを立ち上げます
image.png

今回は簡単に、四則演算を実行するMCPサーバを構築したいと思います。
指示は以下の内容にします。

四則演算を実行するMCPサーバを構築してください。MCPサーバの構築にはmcp.server.fastmcpのFastMCPを使用して作成してください。

「MCPサーバ」とだけ入力すると、MineCraft側のMCPサーバと誤認してしまうので、「mcp.server.fastmcpのFastMCP」と指定します。

すると、以下のように、main.pyというファイルにコードを生成してくれました。
image.png

 │                                                                                                                                                                              │
 │     1 import operator                                                                                                                                                        │
 │     2 from mcp.server.fastmcp import FastMCP                                                                                                                                 │
 │     3                                                                                                                                                                        │
 │     4 # FastMCPサーバーを初期化します                                                                                                                                        │
 │     5 mcp = FastMCP(                                                                                                                                                         │
 │     6     name="calculator_server",                                                                                                                                          │
 │     7     description="四則演算を実行するシンプルな計算機サーバーです。",                                                                                                    │
 │     8     version="0.1.0",                                                                                                                                                   │
 │     9     instructions="加算、減算、乗算、除算のツールを使用して計算を実行します。各ツールは2つの数値を引数に取ります。",                                                    │
 │    10 )                                                                                                                                                                      │
 │    11                                                                                                                                                                        │
 │    12 @mcp.tool(                                                                                                                                                             │
 │    13     name="add",                                                                                                                                                        │
 │    14     description="2つの数値を加算します。",                                                                                                                             │
 │    15     annotations={"a": "最初の数値", "b": "2番目の数値"},                                                                                                               │
 │    16 )                                                                                                                                                                      │
 │    17 def add(a: float, b: float) -> float:                                                                                                                                  │
 │    18     """2つの数値を加算して結果を返します。"""                                                                                                                          │
 │    19     print(f"Adding {a} and {b}")                                                                                                                                       │
 │    20     result = operator.add(a, b)                                                                                                                                        │
 │    21     print(f"Result: {result}")                                                                                                                                         │
 │    22     return result                                                                                                                                                      │
 │    23                                                                                                                                                                        │
 │    24 @mcp.tool(                                                                                                                                                             │
 │    25     name="subtract",                                                                                                                                                   │
 │    26     description="最初の数値から2番目の数値を減算します。",                                                                                                             │
 │    27     annotations={"a": "最初の数値", "b": "2番目の数値"},                                                                                                               │
 │    28 )                                                                                                                                                                      │
 │    29 def subtract(a: float, b: float) -> float:                                                                                                                             │
 │    30     """2つの数値を減算して結果を返します。"""                                                                                                                          │
 │    31     print(f"Subtracting {b} from {a}")                                                                                                                                 │
 │    32     result = operator.sub(a, b)                                                                                                                                        │
 │    33     print(f"Result: {result}")                                                                                                                                         │
 │    34     return result                                                                                                                                                      │
 │    35                                                                                                                                                                        │
 │    36 @mcp.tool(                                                                                                                                                             │
 │    37     name="multiply",                                                                                                                                                   │
 │    38     description="2つの数値を乗算します。",                                                                                                                             │
 │    39     annotations={"a": "最初の数値", "b": "2番目の数値"},                                                                                                               │
 │    40 )                                                                                                                                                                      │
 │    41 def multiply(a: float, b: float) -> float:                                                                                                                             │
 │    42     """2つの数値を乗算して結果を返します。"""                                                                                                                          │
 │    43     print(f"Multiplying {a} and {b}")                                                                                                                                  │
 │    44     result = operator.mul(a, b)                                                                                                                                        │
 │    45     print(f"Result: {result}")                                                                                                                                         │
 │    46     return result                                                                                                                                                      │
 │    47                                                                                                                                                                        │
 │    48 @mcp.tool(                                                                                                                                                             │
 │    49     name="divide",                                                                                                                                                     │
 │    50     description="最初の数値を2番目の数値で除算します。",                                                                                                               │
 │    51     annotations={"a": "被除数", "b": "除数"},                                                                                                                          │
 │    52 )                                                                                                                                                                      │
 │    53 def divide(a: float, b: float) -> float:                                                                                                                               │
 │    54     """2つの数値を除算して結果を返します。ゼロによる除算はエラーになります。"""                                                                                        │
 │    55     print(f"Dividing {a} by {b}")                                                                                                                                      │
 │    56     if b == 0:                                                                                                                                                         │
 │    57         print("Error: Cannot divide by zero.")                                                                                                                         │
 │    58         raise ValueError("ゼロで割ることはできません。")                                                                                                               │
 │    59     result = operator.truediv(a, b)                                                                                                                                    │
 │    60     print(f"Result: {result}")                                                                                                                                         │
 │    61     return result                                                                                                                                                      │
 │    62                                                                                                                                                                        │
 │    63 if __name__ == "__main__":                                                                                                                                             │
 │    64     print("Starting MCP calculator server...")                                                                                                                         │
 │    65     mcp.run()                                                                                                                                                          │

ここまでを書いてもらえましたが、これだとuvコマンドで実行できないことに気づきました。
以下の追加指示で記載を修正してもらいます。

FASTMCPの引数はnameだけにし、mcp.run()の引数はstdioトランスポートとし、uvコマンドで実行してください。

image.png
image.png

MCPサーバが構築できました。

MCPサーバの起動&呼び出し

ClaudeDesktopで呼び出せるようにします。
ClaudeDesktopの左下から設定→開発者の順で画面遷移します。
image.png

「設定を編集」を押下すると、エクスプローラーが開くので、「claude_desktop_config.json」を開きます。
先ほど作成したmain.pyの内容を下記のように記載します。
※隠しているところにはファイルのパスを記載してください。
image.png

記載出来たら、Claudeを再起動します。
再起動すると、追加したものが画面上表示されます。
image.png

実際に呼び出されているか確認してみましょう。

image.png

「ClaudeがcalcのMultiplyを使用します」となっているので、呼び出されていそうです。

image.png

計算してくれました。

4. 最後に

今回は簡単なMCPサーバを構築し呼び出せるかをやってみましたが、複雑なものも大体同じ手順で構築できると思います。

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?